""" Utilities."""
import math
import numpy as np
import pylab as plt


def log_ecdf(seq,normed=False,inverse=False,N=100):
    min_value = min(seq)
    max_value = max(seq)
    print min_value, max_value
    M1 = math.floor(math.log10(min_value))
    M2 = math.ceil(math.log10(max_value))
    bins = 10**np.linspace(M1,M2,N)

    pdf = [0]*N
    for x in seq:
        i = math.log10(x)
        index = int((i-M1)/(M2-M1)*N)
        pdf[index] += 1

    data = sorted((bins[x],pdf[x]) for x in xrange(N))
    x,y = [],[]
    t = 0
    x.append(data[0][0])
    y.append(data[0][1])

    for val,freq in data[1:]:
        x.append(val)
        y.append(y[-1]+freq)

    if normed:
        tot = y[-1]
        y = map(lambda i: 1.0*i/tot, y)

    if inverse:
        y = map(lambda i: 1.0-i,y)

    return x,y

def ecdf(seq,normed=False,inverse=False):
    pdf = {}
    for x in seq:
        if not x in pdf:
            pdf[x] = 0
        pdf[x] += 1

    data = sorted((x,pdf[x]) for x in pdf)
    
    x,y = [],[]
    t = 0
    x.append(data[0][0])
    y.append(data[0][1])

    for val,freq in data[1:]:
        x.append(val)
        y.append(y[-1]+freq)

    if normed:
        tot = y[-1]
        y = map(lambda i: 1.0*i/tot, y)

    if inverse:
        y = map(lambda i: 1.0-i,y)

    return x,y
